package com.ynkbny.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ynkbny.domain.Project;
import com.ynkbny.domain.pojo.ProjectVO;
import com.ynkbny.lgapi.Region;
import com.ynkbny.mapper.ProjectMapper;
import com.ynkbny.service.ProjectService;
import lombok.extern.slf4j.Slf4j;
import org.bson.json.JsonObject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 项目表-表服务实现类
 *
 * @author : juzipi123
 * @date : 2023-7-25
 */
@Slf4j
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements ProjectService {

    @Resource
    private ProjectMapper projectMapper;
    @Resource
    Region region;

    /**
     * @author : juzipi123
     * @return 结果
     */
    @Override
    public List<String> projectLis() {
        log.info("入参：无");
        return projectMapper.projectLis();
    }

    /**
     * 查询 项目表 所有项目的 项目id+项目名字
     *
     * @author : juzipi123
     * @return 项目id+项目名字 list
     */
    @Override
    public List<ProjectVO> projectIdAndName(ProjectVO projectVO) {
        log.info("入参：{}",projectVO);
        List<ProjectVO> projectVOS = projectMapper.projectIdAndName(projectVO);
        log.info("方法返回值：",projectVOS);
        return projectVOS;
    }

    /**
     * @author : juzipi123
     */
    @Override
    public void del() {
        log.info("入参：无");
        projectMapper.del();
    }

    @Override
    public List<com.ynkbny.domain.Region> getRegionsByProjectId(String token, Long projectId) {
        List<com.ynkbny.domain.Region> list = new ArrayList<>();
         JSONArray jsonArray = region.queryRegionByProject(token,projectId).getJSONArray("data");
         for(int i = 0; i < jsonArray.size(); i++){
             com.ynkbny.domain.Region region1 = new com.ynkbny.domain.Region();
             JSONObject object = (JSONObject) jsonArray.get(i);
             region1.setRegionId(object.get("id").toString());
             region1.setProjId(object.get("projId").toString());
             region1.setRegionName(object.get("name").toString());
             list.add(region1);
         }
         return list;
    }
}